package com.hotel.customersystem.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.status.ResultMsg;
import com.common.status.StatusCode;
import com.common.utils.ResultUtils;
import com.common.utils.ResultVo;
import com.hotel.config.websocket.WebSocketProcess;
import com.hotel.customersystem.enums.CusOrderDataStatus;
import com.hotel.customersystem.pojo.CustomerOrder;
import com.hotel.customersystem.pojo.OrderVo;
import com.hotel.customersystem.pojo.CustomerOrderDetailVo;
import com.hotel.customersystem.service.CustomerOrderService;
import com.hotel.customersystem.service.OrderRedisService;
import com.hotel.hotelsystem.pojo.HotelInfo;
import com.hotel.hotelsystem.pojo.RoomInfo;
import com.hotel.hotelsystem.service.HotelInfoService;
import com.hotel.hotelsystem.service.RoomInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.*;

import static com.hotel.config.rabbitmq.config.CutTopConfig.CUTTOP_ORDER_EXCHANGE;
import static com.hotel.config.rabbitmq.config.CutTopConfig.CUTTOP_ORDER_ROUTER_KEY;

/**
 * (CustomerOrder)表控制层
 *
 * @author makejava
 * @since 2022-09-30 16:21:26
 */
@Api(value = "CustomerUserController",tags = {"个人中心订单模块"})
@RestController
@RequestMapping("customerOrder")
public class CustomerOrderController {
    /**
     * 服务对象
     */
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private CustomerOrderService customerOrderService;
    @Resource
    private RoomInfoService roomInfoService;
    @Resource
    private OrderRedisService orderRedisService;


    @ApiOperation(value = "订单列表")
    @GetMapping("/getOrderList/{userId}")
    public ResultVo<List<CustomerOrder>> getOrderList(@PathVariable("userId") Integer userId) {
        List<CustomerOrder> list = customerOrderService.getOrderList(userId);

        if (list.size()!=0) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,list);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,list);

    }

    @Transactional
    @ApiOperation(value = "添加订单")
    @PostMapping("/saveOrder")
    public ResultVo<CustomerOrder> saveOrder(@RequestBody OrderVo orderVo) {
        Boolean haveroom = roomInfoService.haveFitRoom(orderVo.getOrder(),orderVo.getRoomInfo());
        String flag = null;
        if(haveroom){
            //java对象转换成json
            String jsonObject = JSONObject.toJSONString(orderVo);
            String orderNo =(String) rabbitTemplate.convertSendAndReceive(CUTTOP_ORDER_EXCHANGE,CUTTOP_ORDER_ROUTER_KEY,jsonObject,
                    msg -> {
                        msg.getMessageProperties().setDelay(60000);
                        return msg;
                    });
            System.out.println("**************************************\n"+orderNo);
            flag = orderNo;
        }
        System.out.println("*******************flag*******************\n"+flag);
        if (flag!=null&&(!flag.equals("null"))) {
            System.out.println("*******************success*******************\n");
            return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,flag);
        }
        else {
            System.out.println("*******************fail*******************\n");
            return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,flag);
        }
    }

    @ApiOperation(value = "删除订单")
    @PutMapping("/deleteOrder/{orderId}")
    public ResultVo<CustomerOrder> deleteOrder(@PathVariable("orderId") Integer orderId) {
        CustomerOrder order = customerOrderService.getById(orderId);
        order.setOrderDataStatus(0);
        order.setUpdateTime(LocalDateTime.now());
        boolean flag = customerOrderService.updateById(order);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,flag);
    }

    @ApiOperation(value = "取消订单")
    @PutMapping("/cancelOrder/{orderId}")
    public ResultVo<CustomerOrder> cancelOrder(@PathVariable("orderId") Integer orderId) {
        CustomerOrder order = customerOrderService.getById(orderId);
        order.setOrderStatus(CusOrderDataStatus.has_delete.getStatus());
        order.setUpdateTime(LocalDateTime.now());

        //删除redis存放的订单
        orderRedisService.getOrderByIdFromRedis(order.getUserId(),order.getOrderNo());
        boolean flag = customerOrderService.updateById(order);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,flag);
    }

    @ApiOperation(value = "订单详情表")
    @PostMapping("/getDetailOrder")
    public ResultVo<List<CustomerOrder>> getOrderDetail(@RequestBody CustomerOrderDetailVo order) {
        CustomerOrderDetailVo orderDetail = customerOrderService.getOrderDetail(order);
        System.out.println("result:"+orderDetail);
        if (orderDetail!=null) return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,orderDetail);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,orderDetail);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/selectByPage/{userId}/{currentPage}/{pageSize}")
    public ResultVo<IPage<CustomerOrder>> getByPage(@PathVariable("userId") Integer userId,@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize) {
        IPage<CustomerOrder> customerOrderIPage = customerOrderService.selectByPage(userId, currentPage, pageSize);
        if (customerOrderIPage.getTotal() != 0) return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,customerOrderIPage);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,customerOrderIPage);
    }

}

